using System;
using System.Net;
using System.Net.Sockets;


namespace Monorf.Net
{
    public class AddressEndPoint
    {
        private IPEndPoint m_endPoint = null;
        private IPHostEntry m_hostEntry = null;
        private Uri m_myUri;
        private String m_hostname = String.Empty;
        private Int32 m_port = 0;

        //private AsyncCallback completeNameResolver = null;


        public AddressEndPoint()
        {
            m_endPoint = new IPEndPoint(IPAddress.Any, m_port);
            //m_hostname = Dns.GetHostName();
        }

        public AddressEndPoint(Uri uri)
        {
            m_myUri = uri;
            m_port = m_myUri.Port;
            m_hostEntry = Dns.GetHostEntry(m_myUri.Host);

            for (Int32 i = 0; i < m_hostEntry.AddressList.Length; i++)
            {
                if (m_hostEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    m_endPoint = new IPEndPoint(m_hostEntry.AddressList[1], port);
                    break;
                }
            }
        }

        public AddressEndPoint(String addressOrHostname)

        {
            m_hostEntry = Dns.GetHostEntry(addressOrHostname);
            m_endPoint = new IPEndPoint(m_hostEntry.AddressList[0], m_port);
        }

        #region properties 

        /// <summary>
        /// end point to be used
        /// </summary>
        public IPEndPoint EndPoint
        {
            get { return m_endPoint; }
            set { m_endPoint = value; }
        }

        /// <summary>
        /// hostname
        /// </summary>
        public String Hostname
        {
            get { return m_hostname; }
            set { m_hostname = value; }
        }

        #endregion

        #region public methods

        /// <summary>
        /// dummy endpoint to used with Udp
        /// </summary>
        /// <returns></returns>
        public static EndPoint CreateDummyEndPoint()
        {
            IPEndPoint client = new IPEndPoint(IPAddress.Any, 0);
            EndPoint remoteEP = (EndPoint)client;

            return remoteEP;
        }

        #endregion

        #region private methods
        //erere
        #endregion
    }
}
